package com.xiao.xin.service.Impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.xin.domain.ChatHistoryMemory;
import com.xiao.xin.domain.ChatMemory;
import com.xiao.xin.mapper.ChatHistoryMapper;
import com.xiao.xin.service.ChatHistoryService;
import com.xiao.xin.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistoryMemory> implements ChatHistoryService {

    @Override
    public List<String> getChatHistory(String type) {
        List<ChatHistoryMemory> list = list(Wrappers.<ChatHistoryMemory>lambdaQuery().eq(ChatHistoryMemory::getType, type));
        if (CollUtil.isNotEmpty(list)) {
            List<String> ChatmemoryIdList = list.stream().map(ChatHistoryMemory::getChatmemoryId).toList();
            return ChatmemoryIdList;
        }
        return new ArrayList<>();
    }

    @Override
    public void addChatHistory(String type, String chatmemory_id) {
        if (count(Wrappers.<ChatHistoryMemory>lambdaQuery().eq(ChatHistoryMemory::getChatmemoryId, chatmemory_id)) > 0){
            return;
        }
        save(ChatHistoryMemory.builder().type(type).chatmemoryId(chatmemory_id).createTime(LocalDateTime.now()).build());
    }
}
